/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.news;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.news.News;
import com.unis.service.news.NewsService;

/**
 * @author Administrator (email:granzy@sohu.com)
 * @since 2013-02-04
 */
@Controller
@RequestMapping("/news")
public class NewsController {

    private NewsService newsService;

    /****************************************
     * Insert a instace of News
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertNews(@RequestBody News news) throws Exception {
        news.setStatmentId("News.insert");
        String status = "failure";
        newsService.insert(news);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of News
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody News news) throws Exception {
        news.setStatmentId("News.update");
        String status = "failure";
        newsService.update(news);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of News
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody News news) throws Exception {
        news.setStatmentId("News.delete");
        String status = "failure";
        newsService.batchDelete(news);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of News
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        News news = new News();
        news.setStatmentId("News.queryAsList");
        BeanUtils.populate(news, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(news, pageMap);
        List<BaseModel> newsList = newsService.queryAsList(news);
        Map<String, Object> renderMap = GridUIMapper.render(news, newsList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of News
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody News news) throws Exception {
        news.setStatmentId("News.queryAsObject");
        return newsService.queryAsObject(news);
    }

    @RequestMapping("/page")
    public String forword() {
        return "news/newsList";
    }

    @Resource
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }
}
